package icom.icss.service.impl;

import icom.icss.dao.UserDao;
import icom.icss.dao.impl.UserDaoImpl;
import icom.icss.entity.PageBean;
import icom.icss.entity.User;
import icom.icss.service.UserService;

import java.sql.SQLException;

public class UserServiceImpl implements UserService {
    private UserDao  userDao = new UserDaoImpl();

    /**
     * 删除所有用户
     * @param username
     */
    public void DeleteAlluser(String username) throws SQLException {
        userDao.deleteAllUser(username);

    }

    /**
     *  删除
     * @param username
     * @throws SQLException
     */
    public void DeleteByname(String username) throws SQLException {
        userDao.deleteUser(username);
    }

    /**
     * 登入
     * @param username
     * @param password
     * @return
     * @throws SQLException
     */
    public User login(String username, String password) throws SQLException {
        return userDao.selcetByUsernameAndPassword(username,password) ;
    }

    /**
     *
     * @param username
     * @return
     */
    public boolean checkUsername(String username) throws SQLException {
        return userDao.checkUsername(username);
    }

    /**
     *
     * @param user
     */
    public void updateUser(User user) throws SQLException {
        userDao.updatePassword(user);
    }

    @Override
    public User selectUsername(String username) throws SQLException {
        return userDao.selectName(username);
    }

    @Override
    public PageBean selectPageBean(int currentPage, int count) throws SQLException {
        //封装PageBean
        PageBean pageBean = new PageBean();
        //设置当前页
        pageBean.setCurrentPage(currentPage);
        //设置每页显示多少条数据
        pageBean.setCount(count);
        //设置总共多少条记录
        int totalCount = userDao.getTotalCount();
        pageBean.setTotalCount(totalCount);
        //计算总页数
        pageBean.setTotalPage((int)Math.ceil(totalCount/(double)count));
        //设置当前页所有的数据
        pageBean.setUserList(userDao.getUserListByLimit((currentPage-1)*count, count));

        //将pageBean返回
        return pageBean;
    }


}
